#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/uio.h>

#include "rdb_lib.h"
#include "msgbus_lib.h"
#include "base_lib.h"
#include "hdb_lib.h"
#include "usleep.h"
#include "query_his_extern.h"

#define		DBINSERT_LOGIN_USER_NAME	"test"
#define		DBINSERT_LOGIN_PASSWD		"test000"




#define PUB_DAYS_70_YEARS	25567
#define	PUB_SECONDS_PER_DAY	86400

extern class IHdbConnection *m_HdbConn;

int     get_host_time(
	int dtdays,
	int dttime,
	time_t &timer)
{
        int     total_seconds;
        int     seconds_this_day;

        total_seconds = (int) (dtdays - PUB_DAYS_70_YEARS) * PUB_SECONDS_PER_DAY;
        seconds_this_day = (int) (dttime + 150) / 300;

        timer=total_seconds + seconds_this_day;

	return 1;
}



int execute_sql_sentence(char *sql_sentence)
{
	int ret;
	char tmp_str[600],tmp_str1[600];

	//printf("AAAAA SQL:%s\n",sql_sentence);
    sample_dup_info.save_one_report(DUPLICAT_SEND,
            DUPLICAT_SQL_SETTENCE,
            strlen(sql_sentence)+1,
            sql_sentence);

	ret = m_HdbConn->prepare_for_one_sentence((char *)sql_sentence);
	m_HdbConn->execute_one_modify_sentence_commit();
	if( ret < 0 ){
		TRACE_ERROR("error sql: %s\n",sql_sentence);
		printf("error sql: %s\n",sql_sentence);
	}

	return 1;
}

int execute_sql_sentence8(char *sql_sentence) //2000.05.02
{

	int 	ret,i,j,len,end_flag;
	char 	tmp_str[600],tmp_str1[600];
	char	tmp_sen[6000];
	
	//printf("BBBBB SQL:%s\n",sql_sentence);
    sample_dup_info.save_one_report(DUPLICAT_SEND,
            DUPLICAT_SQL_SETTENCE,
            strlen(sql_sentence)+1,
            sql_sentence);

	len = strlen(sql_sentence);
	j = 0;
	for (;j<len;)
	{
		for (i=j+1;i<len;i++)
		if (sql_sentence[i] == '\n' ||
			sql_sentence[i] == 0)
		{
			sql_sentence[i] = 0;
			break;
		}
		
		ret = m_HdbConn->prepare_for_one_sentence((char *)sql_sentence+j);
		m_HdbConn->execute_one_modify_sentence_commit();
		if( ret < 0 ){
			TRACE_ERROR("error sql: %s\n",sql_sentence+j);
			printf("error sql: %s\n",sql_sentence+j);
		}
		j = i+1;
		
	}

	return 1;
}

int execute_sql_sentence9(char *sql_sentence) //2000.05.02
{

	int 	ret,i,j,len,end_flag;
	char 	tmp_str[600],tmp_str1[600];
	char	tmp_sen[6000];
	int     num = 0;

	ret = -1;
	len = strlen(sql_sentence);
	j = 0;
	for (;j<len;)
	{
		for (i=j+1;i<len;i++)
		if (sql_sentence[i] == '\n' ||
			sql_sentence[i] == 0)
		{
			sql_sentence[i] = 0;
			break;
		}
		
		//printf("CCCCCC:%s\n",sql_sentence+j);
		TRACE_INFO("execute_sql_sentence9 insert num = %d\n", num);
		printf("execute_sql_sentence9 insert num = %d\n", num);
        num ++;
		ret = m_HdbConn->prepare_for_one_sentence((char *)sql_sentence+j);
		m_HdbConn->execute_one_modify_sentence_commit();
		if( ret < 0 )
		{
			TRACE_ERROR("error sql: %s\n",sql_sentence+j);
			printf("error sql: %s\n",sql_sentence+j);
			break;
		}
		j = i+1;
		if(num%100==0) usleep(1000);
	}
	TRACE_INFO("execute_sql_sentence9: num = %d\n", num);
	printf("execute_sql_sentence9: num = %d\n", num);
//	if(ret >= 0)
//		ora_conn.commit_transaction();

	return 1;
}

int execute_sql_sentence6(char *sql_sentence)
{
	int ret;
	char tmp_str[600],tmp_str1[600];


//printf("DDDDDDD:%s\n",sql_sentence);
	ret = m_HdbConn->prepare_for_one_sentence((char *)sql_sentence);
	m_HdbConn->execute_one_modify_sentence_commit();
	if( ret < 0 ){
		TRACE_ERROR("error sql: %s\n",sql_sentence);
		printf("error sql: %s\n",sql_sentence);
	}

	return 1;
}
//init_oracle(class system_net_info &sys_info)
int init_oracle(class system_net_info &sys_info,char *proc_name)
{
	char host_name[106];
	struct host_info h_info;
	int ret;

	ret=ns_gethostname(host_name, 100);
	ret=sys_info.get_host_info(host_name,h_info);
	if (ret < 0 || !(h_info.type & DNET_SERVER))
	{
		return	1;
	}

	m_HdbConn->logon((char *)DBINSERT_LOGIN_USER_NAME,
		(char *)DBINSERT_LOGIN_PASSWD,proc_name);
	return 1;
}


int my_host_are_online(class system_net_info &sys_info)
{
	char host_name[106];
	struct host_info h_info;
	int ret;

//return DNET_YES;
	ret=ns_gethostname(host_name, 100);
	ret=sys_info.get_host_info(host_name,h_info);
	if (ret < 0 || !(h_info.type & DNET_SERVER) ||
		h_info.host_attribute != DNET_ONLINE)
	return DNET_NO;

	return DNET_YES;
}



int get_accident_info(
	char 	*table_name,
	char	*time_str1,
	char	*time_str2,
	int	&acc_times,
	time_t	*&acc_ocu_time)
{
	char	sql_statement[600];
	int 	ret_code;
	int 	len,i;
	int	counter,step;
	
	TRACE_INFO("get_accident_info:%s,%s\n",time_str1,time_str2);
	printf("get_accident_info:%s,%s\n",time_str1,time_str2);
	step =200;
	counter = step;
	if (acc_ocu_time ==(time_t *)NULL)
		acc_ocu_time = (time_t *)malloc(counter*sizeof(time_t));
	else
        acc_ocu_time = (time_t *)realloc((void *)acc_ocu_time,
                counter * sizeof(time_t));

	acc_times =0;
	sprintf(sql_statement,"select occur_time from %s where occur_time>= %s and occur_time<=%s order by occur_time",
		table_name,
		time_str1,time_str2);

	RESULT_HEAD2	*result_head = NULL;
	FIELD_INFO	*field_info = NULL;
	CHdbQuery *query = m_HdbConn->get_query();
	int retcode = query->ExecuteQuery(sql_statement, NULL_IS_NULL);
    if (retcode <= 0)
	{
        printf("ExecuteQuery error! sql:%s\n", sql_statement);
        return -1;
    }
	result_head = query->GetQueryHead();
	field_info = query->GetFieldInfo();
	unsigned char is_null;

	if(result_head->data_num <0 || result_head->field_num!=1)
	{
           	printf("get rs info error! rec=%d, col=%d sql=%s\n", result_head->data_num, result_head->field_num, sql_statement);
         	return -1;
	}

	char *cur_pointer;
	int ret;
	for(int row=0; row<result_head->data_num; row++)
	{
		ret = query->GetOneFieldData(is_null, &field_info, &cur_pointer);
		if(ret < 0 || is_null == 1)
		{
           		printf("GetOneFieldData info error! r=%d, c=0\n", row);
         		return -1;
		}

		if (acc_times < counter)
       		memcpy((char*)&acc_ocu_time[acc_times], cur_pointer, sizeof(int));
		else 
		{
			counter += step;
        	acc_ocu_time = (time_t *)realloc((void *)acc_ocu_time,
                		counter * sizeof(time_t));
      		memcpy((char*)&acc_ocu_time[acc_times], cur_pointer, sizeof(int));
		}
		acc_times++;
	}

	return 1;

}


int get_real_table_names(
	int &real_table_num,
	char **real_table_name)
{
	char	sql_statement[600];
	char	tmp_str1[160];
	int 	ret_code;
	int 	len,i;
	
	real_table_num = 0;
	strcpy(sql_statement,"select distinct real_table_name from yc_sample_define");

	RESULT_HEAD2	*result_head = NULL;
	FIELD_INFO	*field_info = NULL;
	CHdbQuery *query = m_HdbConn->get_query();
	int retcode = query->ExecuteQuery(sql_statement, NULL_IS_NULL);
    if (retcode <= 0)
	{
        printf("ExecuteQuery error! sql:%s\n", sql_statement);
        return -1;
    }
	result_head = query->GetQueryHead();
	field_info = query->GetFieldInfo();
	unsigned char is_null;

	if(result_head->data_num <0 || result_head->field_num!=1)
	{
           	printf("get rs info error! rec=%d, col=%d sql=%s\n", result_head->data_num, result_head->field_num, sql_statement);
         	return -1;
	}

	char *cur_pointer;
	int ret;
	for(int row=0; row<result_head->data_num; row++)
	{
		ret = query->GetOneFieldData(is_null, &field_info, &cur_pointer);
		if(ret < 0 || is_null == 1)
		{
           		printf("GetOneFieldData info error! r=%d, c=0\n", row);
         		return -1;
		}
        memcpy(real_table_name[real_table_num], cur_pointer, field_info->field_length);
		real_table_name[real_table_num][field_info->field_length] = 0;
		real_table_num++;
	}

	return 1;

}
